package com.FUp;

import sun.reflect.generics.tree.Tree;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

/**
 * @author 🏹☂࿈正因地࿈🏹️
 * @create 2020/11/9 7:25
 */
public class FilledUpImpl implements IFillUpService{

    private FilledUpImpl(){};

    private static FilledUpImpl filledUp = null;

    public static FilledUpImpl getInstance(){
        if (filledUp==null){
            filledUp = new FilledUpImpl();
        }
        return filledUp;
    }

    @Override
    public long checkSpace(String path) {
        File file = new File(path);
        long totalSpace = file.getTotalSpace();
        long freeSpace = file.getFreeSpace();
        System.out.println("磁盘总空间大小 : " + totalSpace / 1024 / 1024 / 1024 + "G");
        System.out.println("磁盘剩余空间大小 : " + freeSpace / 1024 / 1024 / 1024 + "G");
        return freeSpace;
    }

    @Override
    public boolean isFree(String path) {
        return checkSpace(path)>0;
    }

    @Override
    public void filledUp(String filePath, String targetPath) {
        List<File> fileList = new ArrayList<>();
        File file = new File(filePath);
        fileList.add(file);
        filledUp(fileList,targetPath);
    }

    @Override
    public void filledUp(List<File> fileList, String targetPath) {
        File target = new File(targetPath);
        for (int i = 0; i < quantityManager(fileList,targetPath); i++) {
            File newFolder = new File(target.getAbsolutePath()+i);
            newFolder.mkdirs();
            for (File file : fileList) {
                String command = "cmd /c copy  "+file.getAbsolutePath()+"  "+newFolder.getAbsolutePath();
                System.out.println(command);
                //执行doc命令
                try {
                    Runtime run = Runtime.getRuntime();
                    Process process = run.exec(command);
                } catch (Exception e) {
                    System.out.println("拷贝出现问题");
                }
            }
        }
    }

    @Override
    public List<File> gatherFiles(String folderPath) {
        List<File> fileList = new ArrayList<>();
        File file = new File(folderPath);
        if (file.isDirectory()){
            fileList = readFolder(fileList,file);
        }
        return fileList;
    }

    @Override
    public int quantityManager(List<File> fileList, String targetPath) {
        long fileSize = fileListSize(fileList);
        long freeSpace = checkSpace(targetPath);
        int quantity = 0;
        if (isFree(targetPath) && freeSpace/fileSize>0){
            quantity = (int) (freeSpace/fileSize);
        }
        return quantity;
    }

    @Override
    public int quantityManager(String filePath, String targetPath) {
        List<File> fileList = new ArrayList<>();
        File file = new File(filePath);
        fileList.add(file);
        return quantityManager(fileList,targetPath);
    }


    @Override
    public long fileListSize(List<File> fileList) {
        long totalSize = 0;
        if (fileList.size()>0){
            for (File file : fileList) {
                totalSize += file.length();
            }
        }else {
            System.out.println("无拷贝文件");
        }
        return totalSize;
    }

    @Override
    public List<File> readFolder(List<File> fileList,File folder){
        List<File> filePathList = Arrays.asList(folder.listFiles());
        for (File file : filePathList) {
            if (!file.isFile()){  //判断不出来
                fileList = readFolder(fileList,file);
            }else {
                fileList.add(file);
            }
        }
        return fileList;
    }
}
